package com.iam.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * create by yongli on 2021-06-10 10:13
 */

@Component
@Aspect
public class PerformanceAspect {
    public static final Logger logger = LoggerFactory.getLogger(PerformanceAspect.class);

    /**
     * 切面表达式：
     *
     * @Pointcut("execution(public * cn.hjljy.*.controller..*.*(..))"）
     * public void logCut(){}
     * @Around("logCut()") public Object validateParam(ProceedingJoinPoint joinPoint) throws Throwable {
     * System.out.println("进入切面进行验证");
     * Object obj = joinPoint.proceed();
     * return obj;
     * }
     * @annotation有at符号
     */
    @Pointcut("@annotation(com.iam.aspect.PerformanceAnno)")
    public void pointcut() {

    }


    @Around("pointcut()")
    public Object after(ProceedingJoinPoint joinPoint) {
        Object[] args = joinPoint.getArgs();
        Object obj = new Object();
        long start = System.currentTimeMillis();
        try {
            // 相当于动态执行了这个方法
            obj = joinPoint.proceed(args);
        } catch (Throwable e) {
            e.printStackTrace();
        }
        Long duration = System.currentTimeMillis() - start;
        logger.info("the time is " + duration + "ms");
        return obj;
    }


}
